home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / util / misc / halt.lzh / halt / sdwarn.c < prev    next >
C/C++ Source or Header  |  1995-10-06  |  7KB  |  312 lines

  1. /*
  2.    sdwarn.c --- example shutdown client.
  3.  
  4.    (c) Copyright 1995 SHW Wabnitz
  5.    Written by Bernhard Fastenrath (fasten@shw.com)
  6.  
  7.    This file may be distributed under the terms
  8.    of the GNU General Public License.
  9. */
  10.  
  11. #include <dos.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <exec/types.h>
  16. #include <proto/exec.h>
  17. #include <proto/dos.h>
  18.  
  19. #if defined (__GNUC__)
  20. #include "halt_inline.h"
  21. #else
  22. #include "halt_pragmas.h"
  23. #endif
  24. #include "halt.h"
  25. #include "ups.h"
  26.  
  27. void CloseTimerDevice (void);
  28. int CheckCLISession (void);
  29.  
  30. struct Library *HaltBase = NULL;
  31. struct MsgPort *prt = NULL;
  32. struct timerequest *TimerReq = NULL;
  33. struct MsgPort *TimerPort = NULL;
  34. ULONG TimerMask = 0;
  35. ULONG CliNumber = 0;
  36. BPTR CliStdout = NULL;
  37. struct Process *MyProc;
  38. APTR WindowPtr;
  39.  
  40. char *optarg = NULL;
  41.  
  42. int
  43. getopt (int argc, char *argv[], char *opts)
  44. {
  45.   static int pos = 0;
  46.   char *c;
  47.  
  48.   while (++pos < argc && argv[pos][0] != '-');
  49.   if (pos >= argc)
  50.     return -1;
  51.   if ((c = strchr (opts, (int) argv[pos][1])) == 0)
  52.     return (int) '?';
  53.   if (*(c+1) != ':')
  54.     return (int) *c;
  55.   if (strlen (argv[pos]) > 2)
  56.     optarg = argv[pos] + 2;
  57.   else if (pos+1 < argc)
  58.     optarg = argv[pos+1];
  59.   else
  60.     return (int) '?';
  61.   return (int) *c;
  62. }
  63.  
  64. #if !defined (__GNUC__)
  65. int
  66. SigBreakHandler (void)
  67. {
  68.   struct Message *msg;
  69.  
  70.   if (HaltBase)
  71.   {
  72.     if (prt)
  73.     {
  74.       Forbid ();
  75.       while (msg = GetMsg (prt))
  76.         ReplyMsg (msg);
  77.       RemShutdownPort (prt);
  78.       DeleteMsgPort (prt);
  79.       Permit ();
  80.     }
  81.     CloseTimerDevice ();
  82.     CloseLibrary (HaltBase);
  83.   }
  84.   exit (EXIT_SUCCESS);
  85.   return 0;
  86. }
  87. #endif
  88.  
  89. int
  90. CheckCLISession ()
  91. {
  92.   struct Process *pr;
  93.   int code = 0;
  94.  
  95.   if (!CliNumber)
  96.     return 1;
  97.  
  98.   Forbid ();
  99.   if (pr = FindCliProc (CliNumber))
  100.   {
  101.     /* We might have found a new CLI
  102.        with the same number.
  103.     */
  104.     if (pr -> pr_COS == CliStdout)
  105.       code = 1;
  106.   }
  107.   Permit ();
  108.   return code;
  109. }
  110.  
  111. void
  112. CloseTimerDevice ()
  113. {
  114.   if (!TimerPort)
  115.     return;
  116.  
  117.   AbortIO ((struct IORequest *) TimerReq);
  118.   WaitIO ((struct IORequest *) TimerReq);
  119.   CloseDevice ((struct IORequest *) TimerReq);
  120.   DeleteIORequest (TimerReq);
  121.   DeleteMsgPort (TimerPort);
  122.   TimerPort = NULL;
  123. }
  124.  
  125. void
  126. StartTimer ()
  127. {
  128.   if (TimerPort)
  129.   {
  130.     TimerReq -> tr_node.io_Command = TR_ADDREQUEST;
  131.     TimerReq -> tr_time.tv_secs  = 3;
  132.     TimerReq -> tr_time.tv_micro = 0;
  133.     SendIO ((struct IORequest *) TimerReq);
  134.   }
  135. }
  136.  
  137. ULONG
  138. OpenTimerDevice ()
  139. {
  140.   if (!(TimerPort = CreateMsgPort ()))
  141.     return 0;
  142.  
  143.   if (!(TimerReq = (struct timerequest *)
  144.     CreateIORequest (TimerPort, sizeof (struct timerequest))))
  145.   {
  146.     DeleteMsgPort (TimerPort);
  147.     return 0;
  148.   }
  149.   if (OpenDevice (TIMERNAME, UNIT_VBLANK, (struct IORequest *) TimerReq, 0))
  150.   {
  151.     DeleteIORequest (TimerReq);
  152.     DeleteMsgPort (TimerPort);
  153.     return 0;
  154.   }
  155.   StartTimer ();
  156.   return TimerMask = 1 << TimerPort -> mp_SigBit;
  157. }
  158.  
  159. int
  160. main (int argc, char *argv[])
  161. {
  162.   ShutdownMessage *sm;
  163.   UpsInfo *ups;
  164.   char *UpsEvents[] = UPS_EV_NAMES;
  165.   int abort = 0;
  166.   int verbose = 0;
  167.   ULONG mask, rmask;
  168.   int opt;
  169.  
  170. #if !defined (__GNUC__)
  171.   onbreak (SigBreakHandler);
  172. #endif
  173.  
  174.   MyProc = (struct Process *) FindTask (0);
  175.   WindowPtr = MyProc -> pr_WindowPtr;
  176.   MyProc -> pr_WindowPtr = (void *) -1;
  177.  
  178.   while ((opt = getopt (argc, argv, "ac:v")) != -1)
  179.     switch (opt)
  180.     {
  181.       case 'a': abort = 1; break;
  182.       case 'v': verbose = 1; break;
  183.       case 'c':
  184.         if (CliNumber = atoi (optarg))
  185.         {
  186.           struct Process *pr;
  187.  
  188.           Forbid ();
  189.           if (pr = FindCliProc (CliNumber))
  190.         CliStdout = pr -> pr_COS;
  191.           Permit ();
  192.           if (!CliStdout)
  193.           {
  194.         printf ("Invalid CLI.\n", CliNumber);
  195.         exit ( EXIT_FAILURE );
  196.           }
  197.         }
  198.     break;
  199.       default:
  200.         printf ("Usage: %s [-av] [-c <cli>].\n", argv[0]);
  201.     exit (EXIT_FAILURE);
  202.     }
  203.  
  204.   if (!(HaltBase = OpenLibrary ("halt.library", 0)))
  205.   {
  206.     printf ("Failed to open halt.library.\n");
  207.     return EXIT_FAILURE;
  208.   }
  209.   if (CliNumber)
  210.   {
  211.     if (!OpenTimerDevice ())
  212.     {
  213.       printf ("Failed to open timer.device.\n");
  214.       CloseLibrary (HaltBase);
  215.       return EXIT_FAILURE;    
  216.     }
  217.   }
  218.   if (prt = CreateMsgPort ())
  219.   {
  220.     mask = 1 << prt -> mp_SigBit;
  221.     mask |= SIGBREAKF_CTRL_C;
  222.     mask |= TimerMask;
  223.  
  224.     if (AddShutdownPort (prt))
  225.     {
  226.       while (1)
  227.       {
  228.     rmask = Wait (mask);
  229.     if (rmask & TimerMask)
  230.       StartTimer ();
  231.  
  232.     if (sm = (ShutdownMessage *) GetMsg (prt))
  233.     {
  234.       switch (sm -> sm_Status)
  235.       {
  236.         case SHUTDOWN_WARN:
  237.           printf ("The system is going down in %d seconds.\n", sm -> sm_TimeLeft);
  238.           break;
  239.         case SHUTDOWN_INFO:
  240.           if (verbose)
  241.               {
  242.             if (sm -> sm_Info == SDMI_UPS_MONITOR)
  243.                 {
  244.               printf ("Shutdown info received:\n");
  245.                   ups = sm -> sm_Extra;
  246.                   if (ups -> ui_Event > UPS_EV_MAX)
  247.                     printf ("Event = %d (unknown)\n", ups -> ui_Event);
  248.                   else
  249.                     printf ("Event = %s.\n", UpsEvents[ups -> ui_Event]);
  250.               printf ("Charge = %d.\n", ups -> ui_Charge);
  251.               printf ("Remaining time = %d.\n", ups -> ui_Time);            
  252.                 }
  253.             else
  254.               printf ("Shutdown info received.\n");
  255.               }
  256.           break;
  257.         case SHUTDOWN_ABORT:
  258.           printf ("Shutdown has been aborted.\n");
  259. #if 0
  260.           CloseTimerDevice ();
  261.           RemShutdownPort (prt);
  262.           ReplyMsg ((struct Message *) sm);
  263.           while (sm = GetMsg (prt))
  264.             ReplyMsg ((struct Message *) sm);
  265.           DeleteMsgPort (prt);
  266.           CloseLibrary (HaltBase);
  267.           exit (EXIT_SUCCESS);
  268. #else
  269.           break;
  270. #endif
  271.         case SHUTDOWN_UMOUNT:
  272.           printf ("The system is going down, unmounting filesystems.\n");
  273.           break;
  274.         case SHUTDOWN_NOW:
  275.           printf ("The system is going down in 5 seconds.\n");
  276.           break;
  277.         case SHUTDOWN_HALT:
  278.           printf ("The system is halted.\n");
  279.           break;
  280.         default:
  281.           printf ("Unknown message.\n");
  282.           break;
  283.       }
  284.           if (abort)
  285.             sm -> sm_Flags |= SDMF_CANCEL;
  286.       ReplyMsg ((struct Message *) sm);
  287.     }
  288.         if (rmask & SIGBREAKF_CTRL_C || !CheckCLISession ())
  289.         {
  290.       if (rmask & SIGBREAKF_CTRL_C)
  291.         printf ("CTRL-C\n");
  292.       else
  293.         printf ("\n%s: End of session.\n", argv[0]);
  294.       RemShutdownPort (prt);
  295.       break;
  296.         }
  297.       }
  298.     }
  299.     else
  300.       printf ("AddShutdownPort() failed.\n");
  301.     while (sm = GetMsg (prt))
  302.       ReplyMsg ((struct Message *) sm);
  303.     DeleteMsgPort (prt);
  304.   }
  305.   else
  306.     printf ("Can't allocate message port.\n");
  307.   CloseTimerDevice ();
  308.   CloseLibrary (HaltBase);
  309.   MyProc -> pr_WindowPtr = WindowPtr;
  310.   exit ( EXIT_SUCCESS );
  311. }
  312.